#ifndef __DEVICE_DEF_H_
#define __DEVICE_DEF_H_
#include "libhash.h"
#include "link_socket.h"

typedef struct devHwInfo{
  char          devCpuArch[32];
  unsigned int  devCpuFreq;
  char          devBoardName[32];
  char          devFwInfo[32];
}devHwInfo_t;

typedef enum linkType{
  LINK_485,
  LINK_CAN,
  LINK_TCP,
  LINK_UDP,
}linkType_t;

typedef struct ethLinkInfo{
  char          ipAddr[15];
  char          mask[15];
  char          gateway[15];
  char          mac[17];
  unsigned int  port;
}ethLinkInfo_t;

typedef enum busBaudRate{
  BR_4800       = 4800,
  BR_9600       = 9600,
  BR_38400      = 38400,
  BR_115200     = 115200,
  BR_125000     = 125000,
  BR_25000      = 250000,
  BR_500000     = 500000,
  BR_1000000    = 1000000,
}busBaudRate_t;

typedef struct busLinkinfo{
  unsigned char busNodeId;
  char          charDevNode[16];
  busBaudRate_t buadRate;
}busLinkInfo_t;

typedef struct devNetInfo{
  linkType_t    devLinkType;
  ethLinkInfo_t devEthLinkInfo;
  busLinkInfo_t devBusLinkInfo;
}devNetInfo_t;

typedef enum devStatus{
  INVALID = 0,
  VALID,
  DISCONNECTED,
  CONNECTING,
  CONNECTED,
}devStatus_t;

typedef struct devInfo{
  unsigned char devId;
  unsigned int commErrCnt;
  unsigned int disConCnt;
  devStatus_t   status;
  devHwInfo_t   devHwInfo;
  devNetInfo_t  devNetInfo;
  /* dev specific param */
  void*         spec;
}devInfo_t;

extern devInfo_t devInfoArray[20];

int addDev(unsigned char devId, devInfo_t devInfo);
int getDev(unsigned char devId, devInfo_t *devInfo);
int delDev(unsigned char devId);
int printDev(void);
int clearDev(void);

typedef int (*proFun_t)(ProChan *chan, char *payload, int zise, int op1, int op2);
typedef struct operation {
	proFun_t fun;              /*操作码对应的处理函数*/
}operation_t;

typedef struct devMsg{
  int chanId;
  int op1;
  int op2;
  int errCode;
  char payload[PAY_MAX_SIZE];
}devMsg_t;

extern devMsg_t gDevMsg;
extern pthread_cond_t  gRequirRespondCond;
extern pthread_mutex_t gRequirRespondmutex;
extern bool ackFlag;

int parseDevCmd(void *id, char *payload, int size, 
    int op1, int op2, int errcode, struct frame_list *frames);

int devProtocolInit(void);

#define DEV_API_BASE __attribute__((visibility("default")))

typedef enum devBaseSig{
  BASE_SIG_TIME_STAMP   = 0x02,
}devBaseSig_t;

typedef enum devBaseOpCode1{
  BASE_OP1_RESET_DEV    = 0x01,
  BASE_OP1_SYNC_TIME    = 0x02,
  BASE_OP1_CHECK_ALIEVE = 0x03,
}devBaseOpCode1_t;

typedef struct hwInfo{
  char boardInfoStr[32];
  char fwInfoStr[32];
}hwInfo_t;

typedef struct ethInfo{
  char ipv4Str[16];
  char maskStr[16];
  char gatewayStr[16];
  char mac[12];
}ethInfo_t;

#define ACK_TIMEOUT_MS 10000
int waitAck(unsigned int timeoutMs);

DEV_API_BASE int requirResetDev(int devId);
DEV_API_BASE int requirSyncTime(int devId);
DEV_API_BASE int requirChkAlive(int devId);
DEV_API_BASE int requirGetHwInfo(int devId, hwInfo_t *hwInfo);
DEV_API_BASE int requirGetEthInfo(int devId, ethInfo_t *ethInfo);
DEV_API_BASE int requirSetEthInfo(int devId, ethInfo_t ethInfo);

#endif /*__DEVICE_DEF_H_*/
